


//
//  WZEmojiInputView.swift
//  weibo
//
//  Created by mac on 16/3/7.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit
let KColectionViewIdentifier = "KColectionViewIdentifier"
let KCellRowMaxNumber = 7
let KCellColMaxNumber = 3


class WZEmojiInputView: UIView,UICollectionViewDataSource,UICollectionViewDelegate,WZToolBarViewDelegate {
    
    var flowLayout:UICollectionViewFlowLayout? = UICollectionViewFlowLayout()
    
//    MARK:重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.grayColor()
        setupUI()
        setupColectionView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //    MARK:设置collectionView
   private func setupColectionView(){
    
    collectionView.dataSource = self
    collectionView.delegate = self
    
    collectionView.registerClass(WZUICollectionEmojiCell.self, forCellWithReuseIdentifier: KColectionViewIdentifier)
    

    flowLayout?.scrollDirection = UICollectionViewScrollDirection.Horizontal
    collectionView.pagingEnabled = true
    flowLayout?.itemSize = CGSizeMake(KUIScreenW, 176)
    flowLayout?.minimumInteritemSpacing = 0
    flowLayout?.minimumLineSpacing = 0
    }
    
    //    MARK:实现setupUI方法
    
    private func setupUI(){
        addSubview(toolBar)
        addSubview(collectionView)
        
        toolBar.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.bottom.equalTo(self.snp_bottom)
            make.right.equalTo(self.snp_right)
            make.height.equalTo(40)
        }
//        MARK:设置collectionView的约束
        collectionView.snp_makeConstraints { (make) -> Void in
          make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(toolBar.snp_top)
        }
    }
    
   //    MARK:懒加载
    lazy var toolBar:WZToolBarView = {
        
        let toolbar = WZToolBarView()
        toolbar.delegate = self
        return toolbar
        }()
    lazy var collectionView:UICollectionView = {
        
      let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: self.flowLayout!)
        
    return collectionView
    }()
   
}

//MARK:实现toolbar的代理方法
extension WZEmojiInputView{
    func changEmotionCellShow(buttonType: WZbuttonType) {
        
        switch buttonType {
        case .recent: let index = NSIndexPath(forItem: 0, inSection: 0)
            collectionView.scrollToItemAtIndexPath(index, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
            
        case .defalt: let index = NSIndexPath(forItem: 0, inSection: 1)
        collectionView.scrollToItemAtIndexPath(index, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
            
        case .emoji: let index = NSIndexPath(forItem: 0, inSection: 2)
        collectionView.scrollToItemAtIndexPath(index, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
            
            
        case .lxh: let index = NSIndexPath(forItem: 0, inSection: 3)
        collectionView.scrollToItemAtIndexPath(index, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
            
            
            
            
        }
    }
}

//MARK:实现collectionView的代理方法
extension WZEmojiInputView{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return WZEmojiManager.getAllEmotions().count
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return WZEmojiManager.getAllEmotions()[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(KColectionViewIdentifier, forIndexPath: indexPath) as! WZUICollectionEmojiCell
        
        cell.backgroundColor = UIColor.whiteColor()
    cell.emotions = WZEmojiManager.getAllEmotions()[indexPath.section][indexPath.item]
        return cell
    }
}

class WZUICollectionEmojiCell: UICollectionViewCell {
    
   lazy var emotionButtons:[WZEmotionButton] = [WZEmotionButton]()
    
    var emotions: [WZEmojiModel]?{
        didSet{
            
            for button in emotionButtons{
                button.hidden = true
            }
            
            
            if let Oemotions = emotions{
                
                for (index,emotion)in Oemotions.enumerate(){
 
                    let button = emotionButtons[index]
                     button.addTarget(self, action: "emotionBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
                    button.emotion = emotion
                }
            }
            
            
        }
    }
   
    
    override init(frame: CGRect) {
        super.init(frame: frame)
      
        
        creatEmotionBtn()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //    MARK:实现表情按钮的点击方法
  @objc private func emotionBtnClick(btn:WZEmotionButton){
//       将点击的表情添加到最近使用的表情
    
        WZEmojiManager.addRecentEmotion(btn.emotion)
         NSNotificationCenter.defaultCenter().postNotificationName(KNotificationWriteEmotion, object: self, userInfo: ["userInfo":btn.emotion!])
    
    }
    //    MARK:实现表情栏删除按钮的点击方法
    @objc private func deleteBtnClick(){
        
    }
    
//    创建表情按钮添加到cell上
    private func creatEmotionBtn(){
        
        let itemWith = frame.size.width / CGFloat(7)
        let itemHeight = frame.size.height / CGFloat(3)
        
       
        
        for i in 0..<20{
            let row = i % 7
            let col = i / 7
            let button = WZEmotionButton(type: UIButtonType.Custom)
          
            button.frame = CGRectMake(CGFloat(row) * itemWith,CGFloat(col) * itemHeight,itemWith,itemHeight)
            emotionButtons.append(button)
            button
            contentView.addSubview(button)
            
        }
//        把 删除按钮添加上去
        deleteButton.frame = CGRectMake(6 * CGFloat(itemWith),2 * CGFloat(itemHeight),itemWith,itemHeight-15)
            contentView.addSubview(deleteButton)
        
        
    }
    
//    懒加载删除按钮
    lazy var deleteButton:UIButton = {
    let deleteButton = UIButton(title: nil, image: "compose_emotion_delete", selectedImage: "compose_emotion_delete_highlighted", tag: 0, target: self, action: "deleteBtnClick")
       
        return deleteButton
    }()
    
}
